package icu.d4peng.basic.log.aspect;

import java.util.ArrayList;
import java.util.Arrays;

import icu.d4peng.basic.log.annotation.Log;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Aspect
public class LogAspect {

	private static final Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);

	@Pointcut("@annotation(icu.d4peng.basic.log.annotation.Log)")
	public void annotationLog() {
	}

	@Around("annotationLog()")
	public Object around(ProceedingJoinPoint pjp) throws Throwable {
		// 参数
		ArrayList<String> parameters = new ArrayList<>();
		MethodSignature signature = (MethodSignature) pjp.getSignature();

		// 时间统计
		long beginTime = System.currentTimeMillis();

		// 获得类名称
		String className = pjp.getTarget().getClass().getName();

		// 获得方法名称
		String methodName = signature.getName();

		// 获得入参
		Object[] args = pjp.getArgs();
		parameters.add(Arrays.toString(args));

		// 获得注解
		Log rocLog = signature.getMethod().getAnnotation(Log.class);
		if (rocLog != null) {
			// String values = rocLog.values();
			// parameters.add(values);
		}

		// 返回参数
		Object returnValue = null;

		// 异常记录
		Exception exception = null;
		try {
			returnValue = pjp.proceed();
			return returnValue;
		}
		catch (Exception e) {
			exception = e;
			throw e;
		}
		finally {
			long cost = System.currentTimeMillis() - beginTime;
			if (exception != null) {
				LOGGER.error("[class: {}][method: {}][cost: {}ms][args: {}][exception: {}]", className, methodName,
						cost, parameters, exception);
			}
			else {
				// LOGGER.info("[class: {}][method: {}][cost: {}ms][args: {}][return:
				// {}]", className, methodName, cost,
				// parameters, JSON.toJSONString(returnValue));
			}
		}
	}

}